package net.lainiao.morethread.Chapter2.demo6;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Administrator on 2017/2/28.
 */
public class PrintQueue {
    private Lock queueLock=new ReentrantLock(true);
    public void printJob(Object document){
        queueLock.lock();
        try {
            Long duration=(long) (Math.random()*10000);
            System.out.println(Thread.currentThread().getName()+":PrintQueue1:Printing a Job during "+(duration/1000)+"Seconds");
            Thread.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            queueLock.unlock();
        }
        queueLock.lock();
        try {
            Long duration=(long) (Math.random()*10000);
            System.out.println(Thread.currentThread().getName()+":PrintQueue2:Printing a Job during "+(duration/1000)+"Seconds");
            Thread.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            queueLock.unlock();
        }
    }
}
